package deom4;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2023-04-07
 * Time: 7:04
 */

 class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
public class test14 {
    public ListNode Merge(ListNode list1,ListNode list2) {
        ListNode head1 = new ListNode(-1);
        ListNode cur = head1;
        //两个链表都不为空
        while (list1 != null && list2 != null){
            if(list1.val < list2.val){
                cur.next = list1;
                list1 = list1.next;
            }else {
                cur.next = list2;
                list2 = list2.next;
            }
            cur = cur.next;
        }
        //两个链表有一个为空
        if(list1 != null){
            cur.next = list1;
        }else {
            cur.next = list2;
        }
        return head1.next;

    }
}
